<?php

namespace app\controller;

use app\BaseController;
use app\model\Site;
use think\facade\Cache;
use think\facade\Session;
use app\model\Log;
use app\utils\Utils;
use think\facade\Request;
use think\facade\Db;

class Upload extends BaseController
{
    static public function index()
    {
        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);

        if ($my_user_info['id'] == '') {
            //缺少值
            return json_encode(array("code" => 404, 'msg' => '未登录', 'success' => 0));
        } else {
            $email_time = Cache::get($email, false);
            if (!$email_time) {
                // 不存在当前账号的时间限制缓存
                // 缓存在30秒之后过期
                Cache::set($email, time(), 30);
                // 允许上传
                $files = request()->file();
                try {
                    validate(['image' => 'fileSize:4096|fileExt:jpg,png,svg,bmp,webp,gif,mp4'])
                        ->check($files);
                    $savename = [];
                    foreach ($files as $file) {
                        $savename[] = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
                    }
                    $name_id = md5(time() . $my_user_info['id'] . $savename[0]);
                    $data = ['name_id' => $name_id, 'time' => time(), 'user_id' => $my_user_info['id'], 'url' => 'storage/' . $savename[0], 'times' => 0, 'bad' => 0];
                    Db::name('site_upload')->insert($data);
                    return json_encode(array("code" => 200, 'msg' => '成功', 'success' => 1, 'url' => Site::get('url') . 'file/' . $name_id));
                } catch (\think\exception\ValidateException $e) {
                    echo $e->getMessage();
                }
            } else {
                if ($email_time + 25 < time()) {
                    // 允许上传
                    Cache::set($email, time(), 30);
                    $files = request()->file();
                    try {
                        validate(['image' => 'fileSize:4096|fileExt:jpg,png,svg'])
                            ->check($files);
                        $savename = [];
                        foreach ($files as $file) {
                            $savename[] = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
                        }
                        $name_id = md5(time() . $my_user_info['id'] . $savename[0]);
                        $data = ['name_id' => $name_id, 'time' => time(), 'user_id' => $my_user_info['id'], 'url' => 'storage/' . $savename[0], 'times' => 0, 'bad' => 0];
                        Db::name('site_upload')->insert($data);
                        return json_encode(array("code" => 200, 'msg' => '成功', 'success' => 1, 'url' => Site::get('url') . 'file/' . $name_id));
                    } catch (\think\exception\ValidateException $e) {
                        echo $e->getMessage();
                    }
                } else {
                    return json_encode(array("code" => 404, 'msg' => '请稍等一会儿哦', 'success' => 0));
                }
            }
        }
    }

    static public function file($i = False)
    {
        if (!$i) {
            return json_encode(array("code" => 400, 'msg' => '缺少参数'));
        }
        $select = Db::table('site_upload')->where('name_id', $i)->select()->toArray();
        if (count($select) == 0) {
            return json(array("code" => 404, 'msg' => '不存在'));
        } else {
            $select = $select[array_rand($select)];
            if($select['bad'] != 0){
                return json(array("code" => 404, 'msg' => '不存在'));
            }
            Db::table('site_upload')
                ->where('id', $select['id'])
                ->inc('times')
                ->update();
            return download($select['url'], 'img')->force(false);
        }
    }
    static public function add()
    {
        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);
        if ($my_user_info['id'] == '') {
            return json(array("code" => 403, 'msg' => '未登录'));
        }
        $id = md5(Request::post('id', TRUE));
        $url = Request::post('url', TRUE);
        if ($id || $url) {
            return json(array("code" => 400, 'msg' => '缺少参数'));
        }
        $url = json_decode($url, true);
        $select = Db::table('site_upload')->where('name_id', $id)->select()->toArray();
        if (count($select) == 0) {
            foreach ($url as $item) {
                $data = ['name_id' => $id, 'time' => time(), 'user_id' => $my_user_info['id'], 'url' => $item, 'times' => 0, 'bad' => 0];
                Db::name('site_upload')->insert($data);
            }
            return json(array("code" => 200, 'msg' => '成功'));
        } else {
            if ($select[0]['user_id'] == $my_user_info['id']) {
                foreach ($url as $item) {
                    $data = ['name_id' => $id, 'time' => time(), 'user_id' => $my_user_info['id'], 'url' => $item, 'times' => 0, 'bad' => 0];
                    Db::name('site_upload')->insert($data);
                }
                return json(array("code" => 200, 'msg' => '成功'));
            } else {
                return json(array("code" => 403, 'msg' => '您没有权限操作'));
            }
        }
    }
    static public function view($i = '')
    {
        if ($i == '') {
            return json(array("code" => 400, 'msg' => '缺少参数'));
        }
        $select = Db::table('site_upload')->where('name_id', md5($i))->select()->toArray();
        if (count($select) == 0) {
            return json(array("code" => 400, 'msg' => '不存在'));
        } else {
            return redirect($select[array_rand($select)]['url']);
        }
    }
}
